const client = require('../../client');

// 执行工作流
class InvokeWorkflowRequest{
    constructor(workflowName, executionName, input) {
        if (workflowName == null) {
            throw new Error("workflowName is required");
        }
        this.workflowName = workflowName;
        this.executionName = executionName;
        this.input = input;
    }
}

class InvokeWorkflowHeaders {
    constructor(commonHeaders = {}, regionId = '') {
        this.commonHeaders = commonHeaders;
        this.regionId = regionId;
    }
}

class InvokeWorkflowService {
    constructor(client) {
        this.client = client;
    }

    // 获取函数
    async invokeWorkflow(request, headers) {
        if (!request.workflowName) {
            return new Error('workflowName is required');
        }
        const url = `/openapi/v1/execution/start`; // OPEN API函数详情查询接口URL
        const config = {
            method: 'POST',
            url,
            headers: {
            ...headers.commonHeaders,
            'User-Agent': 'workflow-sdk/v1.0.0',
            'Regionid': headers.regionId,
            'Content-Type': 'application/json'
            },
            params: {},
            data: request, // 即Credentials的入参body
        };
        const res = await this.client.doRequest(config);
        console.log("invoke workflow return: ", res);
        return res; // 返回响应对象
    }
}

module.exports = {
    InvokeWorkflowRequest: InvokeWorkflowRequest,
    InvokeWorkflowHeaders: InvokeWorkflowHeaders,
    InvokeWorkflowService: InvokeWorkflowService,
}